<?php

require_once( "MDB2.php" );

class NewsItem {
	
	public $newsId;
	public $time;
	public $title;
	public $body;
	
	public function getFormattedTime() {
		return date("D, j F, Y", $this->time);
	}
	
}


class NewsItemDAO {
	private $db;
	
	public function __construct( $db = null ) {
		if( $db == null ) {
			$db = MDB2::connect("pgsql://phpfaces_info@localhost/phpfaces_info");
		}
		
		$this->db = $db;	
	}
	
	public function getNewsItems() {
		$newsItems = array();
		
		$sql = "SELECT * FROM news order by time desc";
		
		$res = $this->db->query( $sql );
		while ( $row = $res->fetchRow( MDB2_FETCHMODE_ASSOC ) ) {
			$i = new NewsItem();
			$i->newsId = $row["post_id"];
			$i->title = $row["title"];
			$i->time = strtotime( $row["time"] );
			$i->body = $row["body"];
			
			$newsItems[] = $i;
		}
		
		return $newsItems;
	}
	
}
class NewsBean {

	private $newsItemDAO;
	
	public function __construct() {
		$db = MDB2::connect("pgsql://phpfaces_info@localhost/phpfaces_info");
		$this->newsItemDAO = new NewsItemDAO( $db );
	}

	
	private $recentNewsItems;
	public function getRecentNewsItems() {
		if( $this->recentNewsItems == null ) {
			$this->recentNewsItems = $this->newsItemDAO->getNewsItems();
		}
		return $this->recentNewsItems;
	}
	
	public function viewItem() {
		return "view-item";
	}
}


?>